const cloud = require('wx-server-sdk')

const db =cloud.database()

exports.main = async(event,context)=>{
	let result = await db.collection(event.collectionName).where({
		edge_id:event.edge_id
	}).get();
	let likes = await db.collection('edge_like').where({
		user: event.collectionName == 'edge' ? 'roses_are_rosie' : result.data[0].user.username,
		edge_id:event.edge_id,
		liker:event.liker
	}).get();
	let data = result.data;
	let conss;
	let tag_user;
	try{
		conss = unescape(atob(data['edge_media_to_caption']).replace(/\u/g, "%u")).replace(/\\/g,"");
		tag_user = unescape(atob(data['edge_media_to_tagged_user']).replace(/\u/g, "%u")).replace(/\\/g,"");
	}catch{
		conss = data['edge_media_to_caption']
		tag_user = data['edge_media_to_tagged_user']
	}
	data['edge_media_to_caption'] = conss
	data['edge_media_to_tagged_user'] = tag_user
	data.edgeInfo = likes.data
	return {'msg':'ok','data':data}
}